package com.example.initializesql;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.LinearLayout;

import java.util.ArrayList;

import javax.xml.validation.Validator;

import retrofit2.http.POST;


public class MySql extends SQLiteOpenHelper {




    public MySql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table demo(id integer primary key autoincrement,name text,sex text,age text)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    //添加数据
    public void insert(Bean bean){
        SQLiteDatabase db = getWritableDatabase();
        int id1 = bean.getId();
        String name1 = bean.getName();
        String sex1 = bean.getSex();
        int age1 = bean.getAge();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",id1);
        contentValues.put("name",name1);
        contentValues.put("sex",sex1);
        contentValues.put("age",age1);
        db.insert("demo",null,contentValues);

        db.close();
    }
    //修改数据
    public void UpData(Bean oldbean,Bean newBean){
        SQLiteDatabase db = getWritableDatabase();
        int id = oldbean.getId();
        String name = oldbean.getName();
        String sex = oldbean.getSex();
        int age = oldbean.getAge();
        int id1 = newBean.getId();
        String name1 = newBean.getName();
        String sex1 = newBean.getSex();
        int age1 = newBean.getAge();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",id1);
        contentValues.put("name",name1);
        contentValues.put("sex",sex1);
        contentValues.put("age",age1);
        db.update("demo", contentValues,"id = ? and name = ? and sex = ? and age = ?",new String[]{id+"",name,sex,age+""});
        db.close();
    }
    //删除数据
    public void delete(Bean bean){
        SQLiteDatabase db = getWritableDatabase();
        int id = bean.getId();
        db.delete("demo","id = ?",new String[]{id+""});
        db.close();
    }
    public ArrayList<Bean> Query(){
        SQLiteDatabase db = getWritableDatabase();
        ArrayList<Bean> beans = new ArrayList<>();
        Cursor demo = db.query("demo", null, null, null, null, null, null, null);
        while (demo.moveToNext()){
            int id = demo.getInt(demo.getColumnIndex("id"));
            String name = demo.getString(demo.getColumnIndex("name"));
            String sex = demo.getString(demo.getColumnIndex("sex"));
            int age = demo.getInt(demo.getColumnIndex("age"));
            beans.add(new Bean(id,name,sex,age));
        }
        Log.i("TAG", "Query: "+beans);
        return beans;
    }
}
